//
//  LoginViewController.m
//  ZYZB
//
//  Created by ENERGY on 2018/7/16.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "LoginViewController.h"
#import "NewerRegisterViewController.h"
#import "FindLoginCodeViewController.h"
#import "ConnectWechatViewController.h"
#import "WXApi.h"

static int i = 59;
@interface LoginViewController ()<UINavigationControllerDelegate, UITextFieldDelegate>
@property (nonatomic, strong)UIImageView * iconImgView;
@property (nonatomic, strong)UIButton * messageBtn;//验证码登录
@property (nonatomic, strong)UIButton * codeBtn;//密码登录
@property (nonatomic, strong)UIView * lineView;//按钮下横线
@property (nonatomic, strong)UITextField * phoneTextField;
@property (nonatomic, strong)UITextField * codeTextField;
@property (nonatomic, strong)UIButton * showCodeBtn;//明文按钮
@property (nonatomic, strong)UIButton * getVerificationCodeBtn;//获取验证码
@property (nonatomic, strong)NSString * telNumStr;//电话号码
@property (nonatomic, strong)NSTimer *timmer;
@property (nonatomic, strong)NSString * wxLoginCode;
@property (nonatomic, strong)NSString * logInWay;//1、密码登录  2、短信登录
@end

@implementation LoginViewController(networking)
//登录
- (void)getLoginInformation{
    NSString * timeStr = [self currentTimeStr];
    //str
    NSDictionary * dic = @{
                           
                           @"phone":self.phoneTextField.text,
                           @"t":timeStr,
                           @"userPass":self.codeTextField.text
                           };
    //sign
    NSDictionary * signDic = @{@"phone":self.phoneTextField.text,
                               @"t":timeStr,
                               @"userPass":self.codeTextField.text,
                               @"merchantNo":@"80d709a97af1b8ab"
                               };
    SortMD5Code * md5Code = [[SortMD5Code alloc] init];
    NSString * jsonStr = [md5Code getJsonWithStrDic:dic withSignDic:signDic];
    NSData *postData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:@"http://120.78.157.132:82/index.php/login/index?api=api1001"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    request.HTTPMethod = @"POST";
    //设置请求参数
    request.HTTPBody = postData;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        [self hiddenProgressHUD];
        if (connectionError) {
            if (connectionError.code == -1001) {
                [self.view makeToast:@"请求网络超时，请稍后再试" duration:1 position:CSToastPositionCenter];
            }
        }else{
            NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];//data解析为dictionary
            if (!dictionary) {
                NSLog(@"数据缺失");
                return;
            }
            NSString * compareMD5Str = [md5Code encodeStringWithDictionary:dictionary];
            NSString * signResult = [dictionary objectForKey:@"sign"];//接到的json串sign字段
            NSDictionary * jsonData = [md5Code getNSDictionaryData:dictionary];//解析为字典数据
            
            if ([jsonData[@"code"] isEqualToString:@"000000"]) {
                if ([jsonData[@"body"] count] > 0) {
                    if ([compareMD5Str isEqualToString:signResult]) {
                        NSLog(@"成功");
                        [UserModel sharedInstanced].token = [jsonData[@"body"][0] objectForKey:@"token"];
                        [UserModel sharedInstanced].nickName = [jsonData[@"body"][0] objectForKey:@"custName"];
                        [UserModel sharedInstanced].baiJin = [jsonData[@"body"][0] objectForKey:@"baijin"];//判断是否为白金用户，是的话就是为1，不是的话就是0
                        [UserModel sharedInstanced].mobile = [jsonData[@"body"][0] objectForKey:@"phone"];
                        [self dismissViewControllerAnimated:YES completion:nil];
                    }else{
                        NSLog(@"失败");
                        [self.view makeToast:[NSString stringWithFormat:@"%@", jsonData[@"msg"]] duration:2 position:CSToastPositionCenter style:nil];
                    }
                }else{
                    NSLog(@"无内容");
                    ShowMessage(jsonData[@"msg"]);
                }
            }else{
                ShowMessage(jsonData[@"msg"]);
            }
        }
    }];
}
//微信登录
- (void)reportWXLoginCode{
    NSString * timeStr = [self currentTimeStr];
    //str
    NSDictionary * dic = @{
                           @"code":self.wxLoginCode,
                           @"t":timeStr
                           };
    //sign
    NSDictionary * signDic = @{
                               @"code":self.wxLoginCode,
                               @"t":timeStr,
                               @"merchantNo":@"80d709a97af1b8ab"
                               };
    SortMD5Code * md5Code = [[SortMD5Code alloc] init];
    NSString * jsonStr = [md5Code getJsonWithStrDic:dic withSignDic:signDic];
    NSData *postData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:@"http://120.78.157.132:82/index.php/login/index?api=api1004"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    request.HTTPMethod = @"POST";
    //设置请求参数
    request.HTTPBody = postData;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        [self hiddenProgressHUD];
        if (connectionError) {
            if (connectionError.code == -1001) {
                [self.view makeToast:@"请求网络超时，请稍后再试" duration:1 position:CSToastPositionCenter];
            }else{
                
            }
        }else{
            NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];//data解析为dictionary
            if (!dictionary) {
                NSLog(@"数据缺失");
                return;
            }
//            NSString * compareMD5Str = [md5Code encodeStringWithDictionary:dictionary];
//            NSString * signResult = [dictionary objectForKey:@"sign"];//接到的json串sign字段
            NSDictionary * jsonData = [md5Code getNSDictionaryData:dictionary];//解析为字典数据
            NSLog(@"%@", dictionary);
            if ([[jsonData objectForKey:@"code"] isEqualToString:@"000026"]) {
                ConnectWechatViewController * vc = [[ConnectWechatViewController alloc] init];
                vc.wechatCodeString = [[jsonData objectForKey:@"body"] objectForKey:@"code"];
                [self.navigationController pushViewController:vc animated:YES];
            }
            if ([[jsonData objectForKey:@"code"] isEqualToString:@"000000"]) {
                [UserModel sharedInstanced].token = [jsonData[@"body"] objectForKey:@"token"];
                [UserModel sharedInstanced].nickName = [jsonData[@"body"] objectForKey:@"custName"];
                [UserModel sharedInstanced].baiJin = [jsonData[@"body"] objectForKey:@"baijin"];//判断是否为白金用户，是的话就是为1，不是的话就是0
                [UserModel sharedInstanced].mobile = [jsonData[@"body"] objectForKey:@"phone"];
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }
    }];
}

//短信登录
- (void)msgLogin{
    NSString * timeStr = [self currentTimeStr];
    //str
    NSDictionary * dic = @{
                           
                           @"phone":self.phoneTextField.text,
                           @"t":timeStr,
                           @"phone_code":self.codeTextField.text
                           };
    //sign
    NSDictionary * signDic = @{@"phone":self.phoneTextField.text,
                               @"t":timeStr,
                               @"phone_code":self.codeTextField.text,
                               @"merchantNo":@"80d709a97af1b8ab"
                               };
    SortMD5Code * md5Code = [[SortMD5Code alloc] init];
    NSString * jsonStr = [md5Code getJsonWithStrDic:dic withSignDic:signDic];
    NSData *postData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:@"http://120.78.157.132:82/index.php/login/index?api=api1002"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    request.HTTPMethod = @"POST";
    //设置请求参数
    request.HTTPBody = postData;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        [self hiddenProgressHUD];
        if (connectionError) {
            if (connectionError.code == -1001) {
                [self.view makeToast:@"请求网络超时，请稍后再试" duration:1 position:CSToastPositionCenter];
            }
        }else{
            NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];//data解析为dictionary
            if (!dictionary) {
                NSLog(@"数据缺失");
                return;
            }
            NSString * compareMD5Str = [md5Code encodeStringWithDictionary:dictionary];
            NSString * signResult = [dictionary objectForKey:@"sign"];//接到的json串sign字段
            NSDictionary * jsonData = [md5Code getNSDictionaryData:dictionary];//解析为字典数据
            
            if ([jsonData[@"code"] isEqualToString:@"000000"]) {
                if ([jsonData[@"body"] count] > 0) {
                    if ([compareMD5Str isEqualToString:signResult]) {
                        NSLog(@"成功");
                        //跳页，登录成功
                        [UserModel sharedInstanced].token = [jsonData[@"body"][0] objectForKey:@"token"];
                        [UserModel sharedInstanced].nickName = [jsonData[@"body"][0] objectForKey:@"custName"];
                        [UserModel sharedInstanced].baiJin = [jsonData[@"body"][0] objectForKey:@"baijin"];//判断是否为白金用户，是的话就是为1，不是的话就是0
                        [UserModel sharedInstanced].mobile = [jsonData[@"body"][0] objectForKey:@"phone"];
                        [self dismissViewControllerAnimated:YES completion:nil];
                    }else{
                        NSLog(@"失败");
                        [self.view makeToast:[NSString stringWithFormat:@"%@", jsonData[@"msg"]] duration:2 position:CSToastPositionCenter style:nil];
                    }
                }else{
                    NSLog(@"无内容");
                    ShowMessage(jsonData[@"msg"]);
                }
            }else{
                ShowMessage(jsonData[@"msg"]);
            }
        }
    }];
}
//发送验证码
- (void)sendMessageCode{
    NSString * timeStr = [self currentTimeStr];
    //str
    NSDictionary * dic = @{@"phone":self.phoneTextField.text,
                           @"t":timeStr
                           };
    //sign
    NSDictionary * signDic = @{@"phone":self.phoneTextField.text,
                               @"t":timeStr,
                               @"merchantNo":@"80d709a97af1b8ab"
                               };
    SortMD5Code * md5Code = [[SortMD5Code alloc] init];
    NSString * jsonStr = [md5Code getJsonWithStrDic:dic withSignDic:signDic];
    NSData *postData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:@"http://120.78.157.132:82/index.php/sendCode/index?api=api1001"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    request.HTTPMethod = @"POST";
    //设置请求参数
    request.HTTPBody = postData;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        [self hiddenProgressHUD];
        if (connectionError) {
            if (connectionError.code == -1001) {
                [self.view makeToast:@"请求网络超时，请稍后再试" duration:1 position:CSToastPositionCenter];
            }
        }else{
            NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];//data解析为dictionary
            if (!dictionary) {
                NSLog(@"数据缺失");
                return;
            }
            //        NSString * compareMD5Str = [md5Code encodeStringWithDictionary:dictionary];
            //        NSString * signResult = [dictionary objectForKey:@"sign"];//接到的json串sign字段
            //        NSDictionary * jsonData = [md5Code getNSDictionaryData:dictionary];//解析为字典数据
            //        if ([compareMD5Str isEqualToString:signResult]) {
            //            NSLog(@"成功");
            //        }else{
            //            NSLog(@"失败");
            //        }
        }
    }];
}
@end

@implementation LoginViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;
    [self setStatusBarBackgroundColor:[UIColor whiteColor]];
}
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpViews];
    self.logInWay = @"1";//默认密码登录
    //微信登录
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(WXLogin:) name:WXAuthorizationSuccess object:nil];
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    BOOL isHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isHomePage animated:YES];
}
- (void)setUpViews{
    //图标
    self.iconImgView = [[UIImageView alloc] init];
    //左上角×
    UIImageView * closeImg = [[UIImageView alloc] init];
    int iconW = 150/WIDTH_6_SCALE;
    if (Is_Iphone_X) {
        self.iconImgView.frame = CGRectMake((SCREEN_WIDTH - iconW)/2, 60/WIDTH_6_SCALE + 24, iconW, 43.5/WIDTH_6_SCALE);
        closeImg.frame = CGRectMake(18/WIDTH_6_SCALE, 34/WIDTH_6_SCALE + 24, 15/WIDTH_6_SCALE, 15/WIDTH_6_SCALE);
    }else{
        self.iconImgView.frame = CGRectMake((SCREEN_WIDTH - iconW)/2, 60/WIDTH_6_SCALE, iconW, 43.5/WIDTH_6_SCALE);
        closeImg.frame = CGRectMake(18/WIDTH_6_SCALE, 34/WIDTH_6_SCALE, 15/WIDTH_6_SCALE, 15/WIDTH_6_SCALE);
    }
    self.iconImgView.image = [UIImage imageNamed:@"logo"];
    [self.view addSubview:self.iconImgView];
    closeImg.image = [UIImage imageNamed:@"lend_cross"];
    [self.view addSubview:closeImg];
    UIButton * coverCloseBtn = [[UIButton alloc] initWithFrame:CGRectMake(closeImg.left - 14/WIDTH_6_SCALE, closeImg.top - 14/WIDTH_6_SCALE, closeImg.width + 28/WIDTH_6_SCALE, closeImg.width + 28/WIDTH_6_SCALE)];
    [coverCloseBtn setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:coverCloseBtn];
    [coverCloseBtn addTarget:self action:@selector(closeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    //密码登录
    int codeBtnX = 80/WIDTH_6_SCALE;
    int codeBtnW = 70/WIDTH_6_SCALE;
    self.codeBtn = [[UIButton alloc] initWithFrame:CGRectMake(codeBtnX, self.iconImgView.bottom + 70/WIDTH_6_SCALE, codeBtnW, 15/WIDTH_6_SCALE)];
    [self.codeBtn addTarget:self action:@selector(codeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.codeBtn setTitle:@"密码登录" forState:UIControlStateNormal];
    [self.codeBtn setTitleColor:getColor(@"bdbdbd") forState:UIControlStateNormal];
    [self.codeBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    self.codeBtn.titleLabel.font = DEF_FontSize_14;
    self.codeBtn.selected = YES;
    [self.view addSubview:self.codeBtn];
    //验证码
    self.messageBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - codeBtnW - codeBtnX, self.codeBtn.top, self.codeBtn.width, self.codeBtn.height)];
    [self.messageBtn addTarget:self action:@selector(messageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.messageBtn setTitle:@"短信登录" forState:UIControlStateNormal];
    [self.messageBtn setTitleColor:getColor(@"bdbdbd") forState:UIControlStateNormal];
    [self.messageBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    self.messageBtn.titleLabel.font = DEF_FontSize_14;
    [self.view addSubview:self.messageBtn];
    //横线
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.codeBtn.bottom + 10/WIDTH_6_SCALE, 50/WIDTH_6_SCALE, 2/WIDTH_6_SCALE)];
    self.lineView.backgroundColor = [UIColor orangeColor];
    self.lineView.centerX = self.codeBtn.centerX;
    [self.view addSubview:self.lineView];
    //手机图案
    int phoneImgX = 50/WIDTH_6_SCALE;
    UIImageView * phoneImg = [[UIImageView alloc] initWithFrame:CGRectMake(phoneImgX, self.lineView.bottom + 32/WIDTH_6_SCALE, 12/WIDTH_6_SCALE, 20/WIDTH_6_SCALE)];
    phoneImg.image = [UIImage imageNamed:@"phone"];
    [self.view addSubview:phoneImg];
    //上面的输入框
    self.phoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(phoneImg.right + 12/WIDTH_6_SCALE, 0, SCREEN_WIDTH - phoneImg.right - 12/WIDTH_6_SCALE - phoneImg.left, 22/WIDTH_6_SCALE)];
    self.phoneTextField.delegate = self;
    self.phoneTextField.centerY = phoneImg.centerY;
    self.phoneTextField.font = DEF_FontSize_12;
    self.phoneTextField.placeholder = @"请输入手机号码";
    self.phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.phoneTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:self.phoneTextField];
    //上面横线
    UIView * topLineView = [[UIView alloc] initWithFrame:CGRectMake(phoneImg.left, phoneImg.bottom + 8/WIDTH_6_SCALE, SCREEN_WIDTH - phoneImgX*2, 1/WIDTH_6_SCALE)];
    topLineView.backgroundColor = getColor(grayColor);
    [self.view addSubview:topLineView];
    //锁头图案
    UIImageView * lockImg = [[UIImageView alloc] initWithFrame:CGRectMake(phoneImg.left, topLineView.bottom + 32/WIDTH_6_SCALE, 16/WIDTH_6_SCALE, 18/WIDTH_6_SCALE)];
    lockImg.centerX = phoneImg.centerX;
    lockImg.image = [UIImage imageNamed:@"code"];
    [self.view addSubview:lockImg];
    //下面输入框
    self.codeTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.phoneTextField.left, 0, self.phoneTextField.width, self.phoneTextField.height)];
    self.codeTextField.centerY = lockImg.centerY;
    self.codeTextField.delegate = self;
    self.codeTextField.font = DEF_FontSize_12;
    self.codeTextField.placeholder = @"请输入密码";
    self.codeTextField.secureTextEntry = YES;
    [self.view addSubview:self.codeTextField];
    //明文按钮
    UIButton * showCodeBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.phoneTextField.right - 22/WIDTH_6_SCALE, 0, 20/WIDTH_6_SCALE, 20/WIDTH_6_SCALE)];
    showCodeBtn.centerY = self.codeTextField.centerY;
    [showCodeBtn addTarget:self action:@selector(showCodeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    showCodeBtn.centerY = self.codeTextField.centerY;
    showCodeBtn.selected = YES;
    [showCodeBtn setImage:[UIImage imageNamed:@"hideCode"] forState:UIControlStateNormal];
    [showCodeBtn setImage:[UIImage imageNamed:@"unhideCode"] forState:UIControlStateSelected];
    [self.view addSubview:showCodeBtn];
    self.showCodeBtn = showCodeBtn;
    self.showCodeBtn.hidden = NO;
    //获取验证码按钮
    int getVerificationCodeBtnW = 90/WIDTH_6_SCALE;
    self.getVerificationCodeBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.phoneTextField.right - getVerificationCodeBtnW, showCodeBtn.top, getVerificationCodeBtnW, 30/WIDTH_6_SCALE)];
    self.getVerificationCodeBtn.centerY = self.codeTextField.centerY;
    [self.getVerificationCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.getVerificationCodeBtn addTarget:self action:@selector(getVerificationCodeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.getVerificationCodeBtn setBackgroundColor:[UIColor orangeColor]];
    [self button:self.getVerificationCodeBtn withBGColor:[UIColor orangeColor] withLayerBorderColor:nil withLayerBorderWith:0 withCornerRadius:6 withMasksToBounds:YES withViwe:self.view];
    [self.getVerificationCodeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.getVerificationCodeBtn.titleLabel.font = DEF_FontSize_12;
    self.getVerificationCodeBtn.hidden = YES;
    [self.view addSubview:self.getVerificationCodeBtn];
    //下面横线
    UIView * bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(topLineView.left, lockImg.bottom + 8/WIDTH_6_SCALE, topLineView.width, topLineView.height)];
    bottomLineView.backgroundColor = getColor(grayColor);
    [self.view addSubview:bottomLineView];
    //登录
    int loginBtnX = 48/WIDTH_6_SCALE;
    UIButton * loginBtn = [[UIButton alloc] initWithFrame:CGRectMake(loginBtnX, bottomLineView.bottom + 36/WIDTH_6_SCALE, SCREEN_WIDTH - loginBtnX*2, 37/WIDTH_6_SCALE)];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginBtn.titleLabel.font = DEF_FontSize_14;
    [loginBtn addTarget:self action:@selector(loginBtnCLick:) forControlEvents:UIControlEventTouchUpInside];
    [self button:loginBtn withBGColor:getColor(@"3675bd") withLayerBorderColor:nil withLayerBorderWith:0 withCornerRadius:6 withMasksToBounds:YES withViwe:self.view];
    //忘记密码
    UIButton * forgetCodeBtn = [[UIButton alloc] initWithFrame:CGRectMake(loginBtn.left, loginBtn.bottom + 36/WIDTH_6_SCALE, 60/WIDTH_6_SCALE, 13/WIDTH_6_SCALE)];
    [forgetCodeBtn setBackgroundColor:[UIColor clearColor]];
    [forgetCodeBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
    [forgetCodeBtn setTitleColor:getColor(@"3675bd") forState:UIControlStateNormal];
    forgetCodeBtn.titleLabel.font = DEF_FontSize_12;
    [forgetCodeBtn addTarget:self action:@selector(forgetCodeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetCodeBtn];
    //用微信登录
    int labH = 20/WIDTH_6_SCALE;
    UILabel * wechatLab = [[UILabel alloc] initWithFrame:CGRectMake(42/WIDTH_6_SCALE, SCREEN_HEIGHT - 26/WIDTH_6_SCALE - labH - BOTTOM_HEIGHT, 80/WIDTH_6_SCALE, 18/WIDTH_6_SCALE)];
    wechatLab.text = @"用微信登录";
    wechatLab.font = DEF_FontSize_12;
    wechatLab.textColor = [UIColor greenColor];
    wechatLab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:wechatLab];
    UIImageView * wechatImg = [[UIImageView alloc] initWithFrame:CGRectMake(wechatLab.right + 10/WIDTH_6_SCALE, 0, 19.5/WIDTH_6_SCALE, 15.5/WIDTH_6_SCALE)];
    wechatImg.centerY = wechatLab.centerY;
    wechatImg.image = [UIImage imageNamed:@"wechat"];
    [self.view addSubview:wechatImg];
    UIButton * wechatBtn = [[UIButton alloc] initWithFrame:CGRectMake(wechatLab.left, wechatImg.top, wechatImg.right - wechatLab.left, labH)];
    [wechatBtn addTarget:self action:@selector(wechatBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:wechatBtn];
    //中间竖线
    UIView * middleLineView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2, wechatImg.top, 1/WIDTH_6_SCALE, 16/WIDTH_6_SCALE)];
    middleLineView.backgroundColor = getColor(grayColor);
    [self.view addSubview:middleLineView];
    //新用户注册
    UILabel * newerLab = [[UILabel alloc] initWithFrame:CGRectMake(middleLineView.right + 42/WIDTH_6_SCALE, wechatLab.top, wechatLab.width, wechatLab.height)];
    newerLab.text = @"新用户注册";
    newerLab.font = DEF_FontSize_12;
    newerLab.textColor = getColor(@"3675bd");
    newerLab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:newerLab];
    UIImageView * newerImg = [[UIImageView alloc] initWithFrame:CGRectMake(newerLab.right + 10/WIDTH_6_SCALE, 0, 13.5/WIDTH_6_SCALE, 16.5/WIDTH_6_SCALE)];
    newerImg.centerY = newerLab.centerY;
    newerImg.image = [UIImage imageNamed:@"newerRegister"];
    [self.view addSubview:newerImg];
    UIButton * newerBtn = [[UIButton alloc] initWithFrame:CGRectMake(newerLab.left, newerImg.top, newerImg.right - newerLab.left, labH)];
    [newerBtn addTarget:self action:@selector(newerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:newerBtn];
}
#pragma mark - action
- (void)closeBtnClick:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)showCodeBtnClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    self.codeTextField.secureTextEntry = !self.codeTextField.secureTextEntry;
    NSString* text = self.codeTextField.text;
    self.codeTextField.text = @"";
    self.codeTextField.text = text;
}
- (void)wechatBtnClick:(UIButton *)sender{
    NSLog(@"用微信登录");
//    ConnectWechatViewController * vc = [[ConnectWechatViewController alloc] init];
//    [self.navigationController pushViewController:vc animated:YES];
    
    if ([WXApi isWXAppInstalled]) {
        SendAuthReq * request = [[SendAuthReq alloc] init];
        //request.openID == @0;
        request.state = @"wx_oauth2_authorization_state";
        request.scope = @"snsapi_userinfo";
        [WXApi sendReq:request];
    }
}
- (void)newerBtnClick:(UIButton *)sender{
    NSLog(@"新用户注册");
    NewerRegisterViewController * newerRegisterVC = [[NewerRegisterViewController alloc] init];
    [self.navigationController pushViewController:newerRegisterVC animated:YES];
}
- (void)codeBtnClick:(UIButton *)sender{
    self.showCodeBtn.hidden = NO;
    self.codeBtn.selected = YES;
    self.messageBtn.selected = NO;
    self.getVerificationCodeBtn.hidden = YES;
    [UIView animateWithDuration:0.5 animations:^{
        self.lineView.frame = CGRectMake(0, self.codeBtn.bottom + 10/WIDTH_6_SCALE, 50/WIDTH_6_SCALE, 2/WIDTH_6_SCALE);
        self.lineView.centerX = sender.centerX;
    }];
    self.logInWay = @"1";
}
- (void)messageBtnClick:(UIButton *)sender{
    self.showCodeBtn.hidden = YES;
    self.codeBtn.selected = NO;
    self.messageBtn.selected = YES;
    self.getVerificationCodeBtn.hidden = NO;
    [UIView animateWithDuration:0.5 animations:^{
        self.lineView.frame = CGRectMake(0, self.codeBtn.bottom + 10/WIDTH_6_SCALE, 50/WIDTH_6_SCALE, 2/WIDTH_6_SCALE);
        self.lineView.centerX = sender.centerX;
    }];
    self.logInWay = @"2";
}
- (void)getVerificationCodeBtnClick:(UIButton *)sender{
    NSLog(@"获取验证码");
    if ([self valiMobile:self.phoneTextField.text]) {
        self.telNumStr = self.phoneTextField.text;
//        //获取验证码
        [self sendMessageCode];
        //初始化定时器
        _timmer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(test:) userInfo:nil repeats:YES];
    }else{
        [self.view makeToast:@"请正确填写手机号码" duration:1 position:CSToastPositionCenter style:nil];
        return;
    }
}
- (void)loginBtnCLick:(UIButton *)sender{
    if (self.phoneTextField.text.length < 11) {
        [self.view makeToast:@"请正确填写手机号码" duration:1 position:CSToastPositionCenter style:nil];
        return;
    }
    if ([self.logInWay isEqualToString:@"1"]) {
        if ([self valiMobile:self.phoneTextField.text]) {
            NSLog(@"成功");
            [self getLoginInformation];
        }else{
            [self.view makeToast:@"请正确填写手机号码" duration:1 position:CSToastPositionCenter style:nil];
            return;
        }
    }else{
        [self msgLogin];
    }
}
- (void)forgetCodeBtnClick:(UIButton *)sender{
    NSLog(@"忘记密码");
    FindLoginCodeViewController * vc = [[FindLoginCodeViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)test:(NSTimer *)timmer{
    NSString *str = [NSString stringWithFormat:@"%d后重试",i];
    self.getVerificationCodeBtn.titleLabel.text = str;
    self.getVerificationCodeBtn.userInteractionEnabled = NO;
    i--;
    if (i == -1) {
        [timmer invalidate];
        self.getVerificationCodeBtn.titleLabel.text = @"获取验证码";
        self.getVerificationCodeBtn.userInteractionEnabled = YES;
        i = 59;
    }
}

//微信登录
- (void)WXLogin:(NSNotification *)notification{
    NSDictionary * dic = notification.object;
    self.wxLoginCode = dic[@"code"];
    [self reportWXLoginCode];
}
#pragma mark - textFieldDelegate
- (void)textFieldDidChange :(UITextField *)theTextField{
    if (self.phoneTextField) {
        if (self.phoneTextField.text.length == 11) {
            if ([self valiMobile:self.phoneTextField.text]) {
                NSLog(@"%@", self.phoneTextField.text);
            }else{
                [self.view makeToast:@"请正确填写手机号码" duration:1 position:CSToastPositionCenter style:nil];
                return;
            }
        }
        if (self.phoneTextField.text.length > 11) {
            [self.view makeToast:@"请正确填写手机号码" duration:1 position:CSToastPositionCenter style:nil];
            return;
        }
    }
}
@end
